package 剑指offer;

import leetcode.ListNode;

import javax.jnlp.ClipboardService;

public class Offer18_JZ56_01 {
    // {1,2,3,3,4,4,5}
    // 链表去重
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode node1 = pHead;
        ListNode node2 = node1.next;
        while (node2 != null) {
            if (node1.val != node2.val) {
                node1 = node2;
                node2 = node1.next;
            } else {
                while (node2.val == node1.val && node2.next!=null) {
                    node2 = node2.next;
                }
                node1.next = node2;
                node1 = node2;
                node2 = node1.next;
            }
        }
        return pHead;
    }

    public ListNode deleteDuplication2(ListNode pHead) {
        ListNode root = new ListNode(-1);
        if (pHead==null || pHead.next==null) {
            return pHead;
        }
        ListNode pre = pHead;
        ListNode cur = pHead.next;
        while (cur!=null) {
            if (cur.next!=null && cur.val==cur.next.val) {
                while (cur.next!=null && cur.val==cur.next.val) {
                    cur = cur.next;
                }
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = pre.next;
                cur = cur.next;
            }
        }
        return root.next;
    }
}
